if(DOXYGEN_DOT_FOUND)
  set(OSMSCOUT_HAVE_DOT "YES")
  set(OSMSCOUT_DOT_PATH "${DOXYGEN_DOT_EXECUTABLE}")
else()
  set(OSMSCOUT_HAVE_DOT "NO")
  set(OSMSCOUT_DOT_PATH "")
endif()

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.cmake ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
set(PAGES_FILES Android_Qt.txt Resources.txt StyleSheetSyntax.txt VehiclesAndAccess.txt)
set(PAGES_TITLES "Android/Qt" "Resources" "Style Sheet Syntax" "Vehicles and access")
if(MSVC)
  list(APPEND PAGES_FILES README.VisualStudio.txt)
  list(APPEND PAGES_TITLES "Visual Studio")
endif()
if(MINGW OR MSYS)
  list(APPEND PAGES_FILES README.MSYS2.txt)
  list(APPEND PAGES_TITLES "MSYS2")
endif()
if(APPLE)
  list(APPEND PAGES_FILES README.Apple.txt)
  list(APPEND PAGES_TITLES "Apple")
endif()
if(OSMSCOUT_BUILD_BINDING_JAVA)
  list(APPEND PAGES_FILES README.Java.txt)
  list(APPEND PAGES_TITLES "Java")
endif()
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pages.dox "/**\n\\mainpage libosmscout\n")
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/pages.dox "${TEMPFILECONTENT}\nIn addition to API, this guide provides additional information:\n")
list(LENGTH PAGES_FILES PAGES_COUNT)
math(EXPR PAGES_COUNT "${PAGES_COUNT} - 1")
foreach(PAGE_INDEX RANGE 0 ${PAGES_COUNT})
  file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/pages.dox "- \\ref page${PAGE_INDEX}\n")
endforeach()
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/pages.dox "\n\n")
foreach(PAGE_INDEX RANGE 0 ${PAGES_COUNT})
  list(GET PAGES_TITLES ${PAGE_INDEX} TEMPFILECONTENT)
  file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/pages.dox "\\page page${PAGE_INDEX} ${TEMPFILECONTENT}\n")
  list(GET PAGES_FILES ${PAGE_INDEX} TEMPFILECONTENT)
  file(READ ${CMAKE_CURRENT_SOURCE_DIR}/${TEMPFILECONTENT} TEMPFILECONTENT)
  file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/pages.dox "${TEMPFILECONTENT}\n\n")
endforeach()
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/pages.dox "\n*/\n")

add_custom_target(doc ALL
    COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    COMMENT "Generating API documentation with Doxygen"
    VERBATIM)

set_target_properties(doc PROPERTIES VERSION ${OSMSCOUT_LIBRARY_VERSION} SOVERSION ${OSMSCOUT_LIBRARY_VERSION} FOLDER "Documentation")

if (WIN32 OR APPLE)
  install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html DESTINATION doc)
elseif (UNIX)
  install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html DESTINATION "share/osmscout/docs")
endif ()
